//
//  SDAnswersTestReportBaseClass.m
//
//  Created by   on 2019/3/12
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "SDAnswersTestReportBaseClass.h"
#import "SDAnswersTestReportData.h"


NSString *const kSDAnswersTestReportBaseClassMsg = @"msg";
NSString *const kSDAnswersTestReportBaseClassData = @"data";
NSString *const kSDAnswersTestReportBaseClassStatus = @"status";


@interface SDAnswersTestReportBaseClass ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDAnswersTestReportBaseClass

@synthesize msg = _msg;
@synthesize data = _data;
@synthesize status = _status;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.msg = [self objectOrNilForKey:kSDAnswersTestReportBaseClassMsg fromDictionary:dict];
    NSObject *receivedSDAnswersTestReportData = [dict objectForKey:kSDAnswersTestReportBaseClassData];
    NSMutableArray *parsedSDAnswersTestReportData = [NSMutableArray array];
    
    if ([receivedSDAnswersTestReportData isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDAnswersTestReportData) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDAnswersTestReportData addObject:[SDAnswersTestReportData modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDAnswersTestReportData isKindOfClass:[NSDictionary class]]) {
       [parsedSDAnswersTestReportData addObject:[SDAnswersTestReportData modelObjectWithDictionary:(NSDictionary *)receivedSDAnswersTestReportData]];
    }

    self.data = [NSArray arrayWithArray:parsedSDAnswersTestReportData];
            self.status = [[self objectOrNilForKey:kSDAnswersTestReportBaseClassStatus fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.msg forKey:kSDAnswersTestReportBaseClassMsg];
    NSMutableArray *tempArrayForData = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.data) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForData addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForData addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForData] forKey:kSDAnswersTestReportBaseClassData];
    [mutableDict setValue:[NSNumber numberWithDouble:self.status] forKey:kSDAnswersTestReportBaseClassStatus];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.msg = [aDecoder decodeObjectForKey:kSDAnswersTestReportBaseClassMsg];
    self.data = [aDecoder decodeObjectForKey:kSDAnswersTestReportBaseClassData];
    self.status = [aDecoder decodeDoubleForKey:kSDAnswersTestReportBaseClassStatus];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_msg forKey:kSDAnswersTestReportBaseClassMsg];
    [aCoder encodeObject:_data forKey:kSDAnswersTestReportBaseClassData];
    [aCoder encodeDouble:_status forKey:kSDAnswersTestReportBaseClassStatus];
}

- (id)copyWithZone:(NSZone *)zone {
    SDAnswersTestReportBaseClass *copy = [[SDAnswersTestReportBaseClass alloc] init];
    
    
    
    if (copy) {

        copy.msg = [self.msg copyWithZone:zone];
        copy.data = [self.data copyWithZone:zone];
        copy.status = self.status;
    }
    
    return copy;
}


@end
